<?php
	include_once './init.php';
	include_once './session.php';

	// php的$_POST只接受x-www-form-urlencoded格式的数据
	$method = $_POST["method"];
	// var_dump($HTTP_RAW_POST_DATA);
	switch ($method) {
		case 'login':
			login();
			break;
		case 'isLogin':
			if(isLogin()){
				showmsg($login = array(
					'status' => 'login successfully', 
					'loginStatus' => $_SESSION["loginStatus"],
					'userName' => $_SESSION["userName"],
					'userId' => $_SESSION["userId"]
					)
				);
			}else{
				showmsg($login = array(
					'status' => 'login failed'
					)
				);
			};
			break;
		case 'userList':
			getUserList();
			break;
		case 'videoList':
			getVideoList();
			break;
		case 'deleteUser':
			deleteUser();
			break;
		case 'freeze':
			freeze();
			break;
		case 'deleteVideo':
			deleteVideo();
			break;
		case 'updateDescription':
			updateDescription();
			break;
	}
	function login(){
		$adminName = $_POST["adminName"];
		$adminPwd = $_POST["password"];

		$db = initDatabase();
		$result = $db -> get_row("select * from admin where adminName='$adminName' and adminPwd='$adminPwd'");
		if($result){
			$adminId = $result->adminId;
			$adminName = $result->adminName;
			sessionStart($adminId , $adminName);
			showmsg($login = array(
				'status' => 'login successfully', 
				'loginStatus' => $_SESSION["loginStatus"],
				'adminId' => $_SESSION["adminId"],
				'adminName' => $_SESSION["adminName"]
				)
			);
		}else{
			showmsg($login = array(
				'status' => 'login failed'
				));
		}
	}
	function getUserList(){
		$db = initDatabase();
		$result = $db -> get_results("select * from user");
		if($result){
			showmsg($result);
		}else{
			showmsg($login = array(
				'status' => 'get userList failed'
				));
		}
	}
	function getVideoList(){
		$db = initDatabase();
		$result = $db -> get_results("select * from video");
		if($result){
			showmsg($result);
		}else{
			showmsg($login = array(
				'status' => 'get videoList failed'
				));
		}
	}
	function deleteUser(){
		$userId = $_POST["userId"];
		$db = initDatabase();
		$result = $db -> query("delete from user where userId='$userId'");
		if($result){
			showmsg($delete = array(
				'status' => 'success'
				));
		}else{
			showmsg($delete = array(
				'status' => 'fail'
				));
		}
	}
	function freeze(){
		$videoId = $_POST["videoId"];
		$db = initDatabase();
		$resultOfFreeze = $db -> get_row("select videoStatus from video where videoId='$videoId'");
		if($resultOfFreeze->videoStatus == '0'){
			$resultOfUpdate = $db -> query("update video set videoStatus=1 where videoId='$videoId'");
		}else{
			$resultOfUpdate = $db -> query("update video set videoStatus=0 where videoId='$videoId'");
		}
		if($resultOfUpdate){
			showmsg($freeze = array(
				'status' => 'success'
				));
		}else{
			showmsg($freeze = array(
				'status' => 'fail'
				));
		}
	}
	function deleteVideo(){
		$videoId = $_POST["videoId"];
		$db = initDatabase();
		$row = $db -> get_row("select videoName from video where videoId='$videoId'");
		// row是object, 与关联数组不一样, 需要使用obj->key来获取值.
		$videoPath = 'media/'.$row->videoName;
		$resultOfRemove = unlink($videoPath);
		if($resultOfRemove){
			$resultOfDelete = $db -> query("delete from video where videoId='$videoId'");
			if($resultOfDelete){
				showmsg($delete = array(
				'status' => 'success'
					));
			}else{
				showmsg($delete = array(
					'status' => 'fail'
					));
			}
		}else{
			showmsg($delete = array(
				'status' => 'fail'
				));
		}
	}
	function updateDescription(){
		$videoId = $_POST["videoId"];
		$videoDescription = $_POST["videoDescription"];
		$db = initDatabase();
		$resultOfUpdate = $db -> query("update video set videoDescription='$videoDescription' where videoId='$videoId'");
		if($resultOfUpdate){
			showmsg($delete = array(
				'status' => 'success'
				));		
		}else{
			showmsg($delete = array(
				'status' => 'fail'
				));
		}
	}
?>